package com.ljb.android.component.project.view.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SizeUtils
import com.ljb.android.comm.adapter.decoration.RVItemDecorationMargin
import com.ljb.android.comm.adapter.ktx.addDataAndLast
import com.ljb.android.comm.mvp.CommMvpFragment
import com.ljb.android.comm.router.core.RouterManager
import com.ljb.android.comm.weiget.page.PageState
import com.ljb.android.component.project.R
import com.ljb.android.component.project.adapter.ProjectListAdapter
import com.ljb.android.component.project.bean.ProjectListBean
import com.ljb.android.component.project.common.ProjectConstant
import com.ljb.android.component.project.contract.ProjectListContract
import com.ljb.android.component.project.databinding.ProjectFragmentMainListBinding
import com.ljb.android.component.project.presenter.ProjectListPresenter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.api.RefreshHeader

/**
 * @Author Kotlin MVP Plugin
 * @Date 2021/02/26
 * @Description input description
 **/
class ProjectListFragment :
    CommMvpFragment<ProjectListContract.IPresenter, ProjectFragmentMainListBinding>(),
    ProjectListContract.IView {

    companion object {
        const val KEY_ID = "key_id"
        const val KEY_NAME = "key_name"
    }

    private val mListAdapter = ProjectListAdapter()

    private var mId: String = "0"
    private var mName: String = ""
    private var mPage: Int = 1

    override fun registerPresenter() = ProjectListPresenter::class.java

    override fun getLayoutId() = R.layout.project_fragment_main_list

    override fun registerBinding(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): ProjectFragmentMainListBinding {
        return ProjectFragmentMainListBinding.inflate(inflater, parent, false)
    }

    override fun immersionBarEnabled() = false

    override fun supportDraw() = true

    override fun supportRefresh() = true

    override fun supportLoadMore() = true

    override fun supportPage() = true

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        arguments?.run {
            mId = getString(KEY_ID, "0")
            mName = getString(KEY_NAME, "")
        }
    }

    override fun initView() {
        mBind.rvList.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = mListAdapter
            mListAdapter.setOnItemClickListener { _, _, position ->
                val url = mListAdapter.data[position].link
                goWebView(url)
            }
            addItemDecoration(
                RVItemDecorationMargin(
                    bottom = SizeUtils.dp2px(10f),
                    onlyLastItem = true
                )
            )
        }
    }

    override fun initData() {
        setPage(PageState.STATE_LOADING)
        onRefresh()
    }

    override fun onRefresh() {
        mPage = 1
        getPresenter().getProjectList(mId, mPage)
    }

    override fun onLoadMore() {
        getPresenter().getProjectList(mId, mPage)
    }

    override fun onProjectListSuccess(bean: ProjectListBean) {
        if (mPage == 1) {
            finishRefresh()
            mListAdapter.data.clear()
            mListAdapter.notifyDataSetChanged()

            if (bean.data.datas.isNullOrEmpty()) {
                setPage(PageState.STATE_EMPTY)
            } else {
                setPage(PageState.STATE_SUCCESS)
            }
        } else {
            finishLoadMore(true)
        }

        if (bean.data.datas.isEmpty()) {
            setNoLoadMore(true)
        } else {
            mListAdapter.addDataAndLast(bean.data.datas)
            mPage++
        }
    }

    override fun onProjectListError() {
        if (mPage == 1) {
            finishRefresh(false)
            setPage(PageState.STATE_ERROR)
        } else {
            finishLoadMore(false)
        }
    }

    private fun goWebView(url: String) {
        RouterManager.goWeb(url)
    }

    //使用自定义刷新头
    override fun customRefreshHeader(): RefreshHeader {
        return MaterialHeader(context).apply {
            setColorSchemeResources(
                com.ljb.android.comm.R.color.color_9FFF,
                com.ljb.android.comm.R.color.color_FF3333,
                com.ljb.android.comm.R.color.color_red
            )
        }
    }

    //使用Project tag的 Page状态样式
    override fun pageTag(): String {
        return ProjectConstant.MODULE_NAME
    }

    fun scrollToTop() {
        mBind.rvList.scrollToPosition(0)
    }
}
